<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\Db;
use app\index\controller\User;

const LOGIN = 1;         //登录
const NO_LOGIN = 2;      //没有登录
const SHOP = 3;          //是否有门店
const SUCCESS = 4;       //成功数据
const FAIL = 5;          //失败数据
const PARA_NOT_NULL = 6; //参数不能为空
const FileSavePath = 'upload/';  //文件保存路径
const FileTempPath = 'temp/';    //临时存储
const FileDownLoadDomain = 'https://www.huikezk.com/api/';  //文件下载域名头

//连接mongoDb 返回句柄 $table集合名
function mongo($table)
{
    return Db::connect(Config('mongo'), 'mongo')->table($table);
}
//判断权限 和是否分配权限['login',  'storeid']
function perssion($judge)
{
    if((in_array(LOGIN, $judge) || in_array('login', $judge)) && !User::isLogin()) {
        return RD(NO_LOGIN);
    }
    if((in_array(SHOP, $judge) || in_array('storeid', $judge)) && empty(User::getStoreId())) {
        return RD(FAIL, '没有分配门店');
    }
    return [];
}
//获取查询第一个数据
function getFirst($data)
{
    return $data[0] ?? [];
}
//判断参数是否为空 获取数据
function getInputAll(...$vars)
{
    $data = (!empty($_GET) ? $_GET : $_POST);
    foreach ($vars as $var) {
        if(!array_key_exists($var, $data)) {
            return [];
        }
    }
    return $data;
}
//成功返回值  $result 追加参数  //默认成功
//$info 是 success, fail, nologin
function RD($info = SUCCESS, $result = [])
{
    switch ($info)
    {
        case SUCCESS:
            return ["status"=> 1, "message"=> "成功", 'data'=>$result];
        case FAIL:
            return ["status"=> 0, "message"=> empty($result) ? "失败" : $result];
        case PARA_NOT_NULL:
            return ["status"=> 0, "message"=> '参数不能为空'];
        case NO_LOGIN:
            return ["status"=> -1, "message"=> "没有登录"];
    }
}
//删除数组一个元素返回删除元素数值
function DArray(&$array, $key)
{
    if(isset($array[$key])) {
        $returnValue = $array[$key];
        unset($array[$key]);
        return $returnValue;
    }
    return null;
}
//重命名字段名称
function renameArray(&$array, $key, $newKey)
{
        $returnValue = $array[$key];
        unset($array[$key]);
        $array[$newKey]  = $returnValue;
}
//单文件 上传文件
function uploadFileIn()
{
    $file = request()->file('file');
    if(!empty($file)) {
        $info = $file->move(FileSavePath);
        return FileDownLoadDomain.$info->getPathname();
    }
    return '';
}

//内部删除文件
function deleteFileIn($path)
{
    if(!empty($path)) {
        $path = str_replace(FileDownLoadDomain, "",$path);
        if(file_exists($path)) {
            unlink($path);
        }
        $a=@array_diff(scandir(dirname($path)),array('..','.'));
        if(empty($a)) {
            @rmdir(dirname($path));
        }
        return  true;
    }
    return false;
}
//只替换字符串一次
function str_replace_once($needle, $replace, $haystack)
{
    $pos = strpos($haystack, $needle);
    if ($pos === false) {
        return $haystack;
    }
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}
//获取数组 分页
function array_paging(&$array, $start, $length)
{
    if($start + $length > count($array)) {
        $length = count($array) - $start;
    }
    $array = array_slice($array, $start, $length);
}
//追加数据到 数组[0: 1: ]
function array_append($array1, $array2)
{
    foreach ($array2 as $key=>$value) {
        array_push($array1, $value);
    }
    return $array1;
}

function uuid()
{
    return session_create_id(uniqid());
}
//base64转为图片
function base64_image_content($base64_image_content,$path){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path.'.'.$type;
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            return $new_file;
        } else {
            return false;
        }
    }
    return false;
}
//图片转base64
function base64EncodeImage($image_file)
{
    $base64_image = '';
    $image_info = getimagesize($image_file);
    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
    $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
    return $base64_image;
}